package cc.rengu.igas.cops.core.service.trans.asyncall;
/*
 * *异步处理微信推送信息结果
 *Create by fuyp on 2020-4-9
 * Update by zhangbin on 2020-6-12
 */

import cc.rengu.igas.channel.wechat.facade.request.FinancialTransMsgPushRequest;
import cc.rengu.igas.channel.wechat.facade.response.FinancialTransMsgPushResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.igas.cops.common.constant.CopsTreeNodeConstant;
import cc.rengu.igas.cops.common.dao.CopsDevBindInfoMapper;
import cc.rengu.igas.cops.common.dao.impl.CopsDevBindInfoMapperImpl;
import cc.rengu.igas.cops.common.entity.CopsDevBindInfo;
import cc.rengu.igas.cops.common.entity.CopsMchntBaseInfo;
import cc.rengu.igas.cops.common.enums.RespCodeEnum;
import cc.rengu.igas.smps.facade.request.GeneralSmsNoticeRequest;
import cc.rengu.igas.smps.facade.request.InspiryVoicePushRequest;
import cc.rengu.oltp.service.base.DubboAsynBaseService;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.dao.MsgTemplateCfgMapper;
import cc.rengu.oltp.service.common.dao.impl.MsgTemplateCfgMapperImpl;
import cc.rengu.oltp.service.common.entity.MsgTemplateCfg;
import cc.rengu.oltp.service.common.entity.PayOrderInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.annotation.BIZ_PROCESS;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class CopsDealFinancialTransMsgPush extends DubboAsynBaseService {
    @BIZ_PROCESS
    public void dealFinancialTransMsgPush() throws Exception {
        rglog.debug(">>>>>>调用微信推送处理结果<<<<<<");
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String mchntNo = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.MCHNT_NO);
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        FinancialTransMsgPushRequest request = (FinancialTransMsgPushRequest) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_REQUEST, FinancialTransMsgPushRequest.class);
        if (null == request) {
            rglog.debug("请求报文异常");
            throw new BizException(OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), OltpRpcdEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        Result<FinancialTransMsgPushResponse> financialTransMsgPushResponseResult = (Result) xmlTreeUtil.getXmlTreeObjectValue(TreeNodeConstant.DUBBO_RESPONSE, Result.class);
        /*jsonobject转成javaobject节点处理*/
        FinancialTransMsgPushResponse response = (FinancialTransMsgPushResponse) JSONObject.parseObject(JSONObject.toJSONString(financialTransMsgPushResponseResult.getResult()), FinancialTransMsgPushResponse.class);
        /*获取虚拟账户订单表*/
        PayOrderInfo virPayOrederInfo = (PayOrderInfo) xmlTreeUtil.getXmlTreeObjectValue(CopsTreeNodeConstant.VIRT_ACCT_PAY_OORDER_INFO, PayOrderInfo.class);
        /*初始化dubbo服务*/
        DubboService dubboService = new DubboServiceImpl();
        /*微信推送成功 后 继续推送 语音设备*/
        if (financialTransMsgPushResponseResult.isSuccess() && RespCodeEnum.TRANS_SUCCESS.getRespCode().equals(response.getRespCode())) {
            /*主微信推送dubbo调用成功*/
            rglog.info("主微信推送成功mchnt:{} sysSeqNum{}", mchntNo, sysSeqNum);
            /*初始化语音设备推送请求报文*/
            InspiryVoicePushRequest inspiryVoicePushRequest = new InspiryVoicePushRequest();
            /*设置 播报金额 单位元 */
            inspiryVoicePushRequest.setPayAmt(virPayOrederInfo.getOrderAmt());
            /*设置交易类型 0-支付或消费；1-撤销；2-退货*/
            inspiryVoicePushRequest.setPayType("0");
            /*设置 通道调用类型*/
            inspiryVoicePushRequest.setPayType("2");
            /*设置 交易流水号*/
            inspiryVoicePushRequest.setPaySeqNum(response.getHeader().getTraceNo());
            /*获取设备绑定信息*/
            CopsDevBindInfoMapper devBindInfoMapper = new CopsDevBindInfoMapperImpl();
            List<CopsDevBindInfo> devBindInfoList = devBindInfoMapper.getDevBindInfoByMchntNo(instId, "02", mchntNo);
            for (CopsDevBindInfo devBindInfo : devBindInfoList) {
                /*设置产品key*/
                inspiryVoicePushRequest.setProductKey(devBindInfo.getBindDevBrand());
                /*设置设别id*/
                inspiryVoicePushRequest.setDeviceName(devBindInfo.getBindDevId());
                /*异步调用dubbo服务 推送信息到语音设备*/
                dubboService.asynCallDubboService("msgPushService", "inspiryVoicePush", inspiryVoicePushRequest, null);
            }

        }
        /*推送短息处理逻辑*/
        /*主微信推送失败 推送短信到客户手机*/
        else {
            /*主微信推送dubbo调用失败*/
            rglog.info("主微信推送失败mchnt:{} sysSeqNum{}", mchntNo, sysSeqNum);
            /*获取消息模板*/
            MsgTemplateCfgMapper msgTemplateCfgMapper = new MsgTemplateCfgMapperImpl();
            MsgTemplateCfg msgTemplateCfg = msgTemplateCfgMapper.selectMsgTemplateCfgByTemplateType(instId, "SMS02");
            /*获取商户基本信息*/
            CopsMchntBaseInfo mchntBaseInfo = (CopsMchntBaseInfo) xmlTreeUtil.getXmlTreeObjectValue(CopsTreeNodeConstant.MCHNT_BASE_INFO, CopsMchntBaseInfo.class);
            /*获取通道支付订单*/
            /*初始化 短信信息推送请求*/
            GeneralSmsNoticeRequest generalSmsNoticeRequest = new GeneralSmsNoticeRequest();
            /*设置header*/
            cc.rengu.igas.smps.facade.base.Header header = new cc.rengu.igas.smps.facade.base.Header();
            BeanUtil.beanCopy(response.getHeader(), header);
            header.setVersion("1.0.0");
            header.setTxnNum("00450000000E01");
            generalSmsNoticeRequest.setHeader(header);
            /*短信模板*/
            generalSmsNoticeRequest.setTemplateId(msgTemplateCfg.getTemplateId());
            /**实时标志*/
            generalSmsNoticeRequest.setRealtimeFlag(AppParamConstant.YES);
            /**预约时间*/
            generalSmsNoticeRequest.setAppointTime("0");
            /**手机号最多5个，以逗号分隔 */
            List<String> phoneNoList = new ArrayList<>();
            phoneNoList.add(mchntBaseInfo.getMchntContactPhone());
            generalSmsNoticeRequest.setPhoneList(phoneNoList);
            /**短信内容 */
            generalSmsNoticeRequest.setSmsContent("收款" + virPayOrederInfo.getOrderAmt() + "元");

            /*调用短信推送*/
            dubboService.asynCallDubboService("smsPlatService", "smsMsgPush", generalSmsNoticeRequest, null);
        }
    }
}